package com.scm.biz.action;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.scm.biz.bean.OrderTaskPerformer;
import com.scm.biz.bean.ProductionPattern;
import com.scm.biz.bean.SysUser;
import com.scm.biz.dao.OrderTaskPerformerDao;
import com.scm.biz.dto.ProductionPatternDto;
import com.scm.biz.enums.FileCategoryEnum;
import com.scm.biz.enums.OrderStatusEnum;
import com.scm.biz.enums.OrderTypeEnum;
import com.scm.biz.service.BusinessFileService;
import com.scm.biz.service.FactoryService;
import com.scm.biz.service.OrderTaskPerformerService;
import com.scm.biz.service.ProductionOrderService;
import com.scm.biz.service.ProductionPatternService;
import com.scm.core.bean.exp.UfdmException;
import com.scm.core.bean.resp.RespBean;
import com.scm.utils.UfdmDateUtil;
@Controller
@RequestMapping("/productionpattern")
public class ProductionPatternAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3112673910837625784L;
	@Resource
	ProductionPatternService productionPatternService;
	@Resource
	BusinessFileService businessFileService;
	@Resource
	FactoryService factoryService;
	@Autowired
	OrderTaskPerformerService orderTaskPerformerService;
	@Autowired
	ProductionOrderService productionOrderService;
	@RequestMapping("/saveNotice")
	@ResponseBody
	public RespBean<String> saveNotice(ProductionPatternDto productionPatternDto){	
		RespBean<String> respBean = new RespBean<String>();
		return respBean;
	}

	@RequestMapping("/saveTM")
	@ResponseBody
	public RespBean<String> saveTM(Long id,MultipartFile file) throws UfdmException{	
		if(file == null){
			throw new UfdmException("请选择文件");
		}
		ProductionPattern productionPattern = productionPatternService.selectById(id);	
	
		productionPatternService.saveTmFile(productionPattern,file);
		RespBean<String> respBean = new RespBean<String>();
		return respBean;
	}
	@RequestMapping("/addLastReport")
	public ModelAndView addLastReport(Long productionId){
		ModelAndView mvAndView = new ModelAndView("order/production_last_report_add");
		mvAndView.addObject("productionPatternId",productionId);
		mvAndView.addObject("lastFiles",businessFileService.selectListByBusinessId(productionId, FileCategoryEnum.PROOFING_LAST_FILE.value()));
		mvAndView.addObject("middleFiles",businessFileService.selectListByBusinessId(productionId, FileCategoryEnum.PROOFING_MIDDLE_FILE.value()));
		
		mvAndView.addObject("productionPattern",productionPatternService.selectById(productionId));
		return mvAndView;
	}
	@RequestMapping("/saveLastReport")
	@ResponseBody
	public RespBean<String> saveLastReport(ProductionPatternDto productionPatternDto,MultipartFile[] middleFile, MultipartFile[] lastFile) throws UfdmException{	
		
		ProductionPattern productionPattern = productionPatternService.selectById(productionPatternDto.getId());
		productionPattern.setLastCheckFinish(1);
		productionPattern.setMiddleCheckStarttime(UfdmDateUtil.stringToDate(productionPatternDto.getMiddleCheckStartime(), "yyyy-MM-dd HH:mm"));
		productionPattern.setMiddleCheckEndtime(UfdmDateUtil.stringToDate(productionPatternDto.getMiddleCheckEndime(), "yyyy-MM-dd HH:mm"));
		productionPattern.setUploadMiddleMemo(productionPatternDto.getUploadMiddleMemo());
		productionPattern.setLastCheckTime(UfdmDateUtil.stringToDate(productionPatternDto.getLastCheckTime(), "yyyy-MM-dd HH:mm"));
		productionPattern.setUploadLastMemo(productionPatternDto.getUploadLastMemo());
	
		productionPatternService.saveLastProduce(productionPattern, lastFile,middleFile);
		RespBean<String> respBean = new RespBean<String>();
		return respBean;
	}
	@RequestMapping("/addProduceDate")
	public ModelAndView addProduceDate(Long productionId){
		ModelAndView mvAndView = new ModelAndView("order/production_produce_date_add");
		mvAndView.addObject("productionPatternId",productionId);
		mvAndView.addObject("productionPattern",productionPatternService.selectById(productionId));
		return mvAndView;
	}
	@RequestMapping("/saveProduceDate")
	@ResponseBody
	public RespBean<String> saveProduceDate(ProductionPatternDto productionPatternDto){	
		
		ProductionPattern productionPattern = productionPatternService.selectById(productionPatternDto.getId());
		productionPattern.setMaterialExpire(UfdmDateUtil.stringToDate(productionPatternDto.getMaterialExpire(), "yyyy-MM-dd HH:mm"));
		productionPattern.setProduceStartdate(UfdmDateUtil.stringToDate(productionPatternDto.getProduceStartdate(), "yyyy-MM-dd HH:mm"));
		productionPattern.setProduceEnddate(UfdmDateUtil.stringToDate(productionPatternDto.getProduceEnddate(), "yyyy-MM-dd HH:mm"));
		if("1".equals(productionPatternDto.getIsfirstSampleChk())){
			productionPattern.setFirstSample(UfdmDateUtil.stringToDate(productionPatternDto.getFirstSample(), "yyyy-MM-dd HH:mm"));
				
		}else{
			productionPattern.setFirstSample(null);
			
		}
		productionPattern.setFirstSampleMemo(productionPatternDto.getFirstSampleMemo());
		productionPatternService.saveProduceDate(productionPattern);
		RespBean<String> respBean = new RespBean<String>();
		return respBean;
	}
	@RequestMapping("/saveContractAdd")
	@ResponseBody
	public RespBean<String> saveContractAdd(Long id,MultipartFile file) throws UfdmException{	
		businessFileService.insert(file, id, FileCategoryEnum.PRODUCTION_CONTRACT_FILE.value(),0);
		RespBean<String> respBean = new RespBean<String>();
		return respBean;
	}
	@RequestMapping("/saveFee")
	@ResponseBody
	public RespBean<String> saveFee(Long id){	
		ProductionPattern productionPattern = productionPatternService.selectById(id);
		productionPattern.setStatus(OrderStatusEnum.FINISH.value());
		productionPatternService.update(productionPattern);
		
		SysUser sysUser = (SysUser)SecurityUtils.getSubject().getSession().getAttribute("sysUser");

		
		OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
		orderTaskPerformer.setAction("F305");
		orderTaskPerformer.setBusinessId(productionPattern.getId());
		orderTaskPerformer.setBusinessType(OrderTypeEnum.T3.toString());
		orderTaskPerformer.setCreatedAt(new Date());
		orderTaskPerformer.setPerformer(sysUser.getId());
		orderTaskPerformer.setUpdatedAt(new Date());
		orderTaskPerformer.setOrderId(productionOrderService.selectByPk(productionPattern.getProductionOrderId()).getOrderId());
		orderTaskPerformerService.addPerformer(orderTaskPerformer);
		RespBean<String> respBean = new RespBean<String>();
		return respBean;
	}
}
